<template>
  <div class="searchResults">
    <van-list
      v-model="loading"
      :finished="finished"
      :error.sync="error"
      error-text="请求失败，点击重新加载"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.art_id" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from '@/api/search.js'
export default {
  name: 'searchResults',
  props: {
    searchText: {
      type: String,
      required: true
    }

  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      error: false

    }
  },

  created () {},

  methods: {
    async onLoad () {
      // 1.发送请求
      try {
        const res = await getSearchResult({
          page: this.page,
          pre_page: 10,
          q: this.searchText
        })
        // console.log(res)
        if (Math.random() < 0.5) {
          console.lg(123)
        }
        // 2、保存数据
        this.list.push(...res.results)
        // this.list.push(this.list.length + 1)
        // 加载状态结束
        this.loading = false
        // 数据全部加载完成
        if (!res.results.length) {
          this.finished = true
        } else {
          this.page++
        }
      } catch (err) {
        this.error = true
        this.loading = false
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
